<?php

namespace app\index\controller;

use app\BaseController;
use app\index\model\Article;
use app\index\model\Category as ModelCategory;
use think\facade\Db;
use think\facade\Session;

class Category extends BaseController
{
    public function index()
    {
        $this->logs('一级分类首页');
        $list = (new ModelCategory())->list();
        $uid = $this->user['user_id'] ?? 0;
        $followLis = (new ModelCategory)->follow($uid);
        return view('index', ['list' => $list , 'followCate' => $followLis]);
    }
    public function detail()
    {
        $this->logs('二级分类及三级分类文章列表');
        $param = input();
        $category_id = isset($param['cid']) ? $param['cid'] : '';
        $uid = $this->user['user_id'] ?? 0;
        $hasFollow = Db::name('cateFollow')->where(['cate_id' => $category_id , 'user_id' => $uid])->find();
        if($hasFollow == null ||  $hasFollow['delete_time'] == 0){
            $hasFollow = false;
        }else{
            $hasFollow = true;
        }
        // 第一次传来的 ID 和 后面传来的ID 
        $ref = request()->header('referer');
        if ($ref == 'http://' . request()->host() . '/index/category/index.html') {
            Session::set('cate.id', $category_id);
        }
        $type = input('type', 0);
        // 查询父类是否存在
        $cateData = ModelCategory::findOrEmpty($category_id);
        if ($cateData->isEmpty()) return view('error/404', ['msg' => '该分类不存在']);
        // 判断是否是三级分类请求列表
        if ((new ModelCategory())->isThree($category_id) == 1) {
            $cateArr = $category_id;
        } else {
            $threeCateIdArr = ModelCategory::where(['pid' => ModelCategory::find($category_id)->id, 'is_show' => 1])->column('id');
            $cateArr = $threeCateIdArr;
        }
         if (Session::has('cate.id')) {
            $category_id = Session::get('cate.id');
        }
        $list = (new Article())->list(['limit' => 5, 'category_id' => $cateArr, 'status' => 1,'parentCid' => $category_id , 'type' => $type]);
        $data = ModelCategory::find($category_id);
        $threeList = (new ModelCategory())->threeChild($category_id);
        $page = $list['list']->render();
        return view('detail', ['data' => $data, 'list' => $list, 'page' => $page, 'threeList' => $threeList ,'hasFollow' => $hasFollow,'type' => $type]);
    }
    public function follow()
    {
        $cid = input('cid');
        if (empty($cid)) return error('参数CID不能为空');
        $hasFollow = Db::name('cateFollow')->where(['user_id' => $this->user['user_id'], 'cate_id' => $cid]);
        // 取消关注
        if ($hasFollow->find() && $hasFollow->find()['delete_time'] == 0) {
            $res = Db::name('cateFollow')->where(['user_id' => $this->user['user_id'], 'cate_id' => $cid])->update(['delete_time' => time()]);
        } 
        // 关注
        else if($hasFollow->find() && $hasFollow->find()['delete_time'] != 0){
            $res = Db::name('cateFollow')->where(['user_id' => $this->user['user_id'], 'cate_id' => $cid])->update(['delete_time' => 0]);
        }
        // 新增记录
        else {
            $res = $hasFollow->insert(['user_id' => $this->user['user_id'], 'cate_id' => $cid,'delete_time' => time()]);
        }
        if ($res) return success();
        return error();
    }
}
